package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import javax.servlet.http.HttpSession;

import server.ForumAPIService;
import forum.Forum;
import forum.Member;
import forum.Post;

public class DBHandler implements ForumAPIService {

	private Connection conn;
	private RequestParser rp;

	public DBHandler() {
		conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager
							.getConnection("jdbc:mysql://localhost:3306/forum","root","2598");

			rp = new RequestParser(conn);
		} catch (Exception e) {
			System.out.println(e);
			System.out.println("Please check the database IP");
			System.exit(0);
		}
	}

	//DBHandler implementation
	
	/* (non-Javadoc)
	 * @see database.DatabaseInterface#getFriends(javax.servlet.http.HttpSession)
	 */
	@Override
	public List<Member> getFriends(HttpSession session) {
		return rp.parseGetFriends((String)session.getAttribute("username"));
	}
	
	public List<Member> getFriends(String username) {
		return rp.parseGetFriends(username);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#addFriend(javax.servlet.http.HttpSession, java.lang.String)
	 */
	@Override
	public int addFriend(HttpSession session, String friendName) {
		return rp.parseAddFriend((String)session.getAttribute("username"), friendName);
	}

	public int addFriend(String username, String friendName) {
		return rp.parseAddFriend(username, friendName);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#removeFriend(javax.servlet.http.HttpSession, java.lang.String)
	 */
	@Override
	public int removeFriend(HttpSession session, String friendName) {
		return rp.parseRemoveFriend((String)session.getAttribute("username"), friendName);
	}
	
	public int removeFriend(String username, String friendName) {
		return rp.parseRemoveFriend(username, friendName);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#addUser(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, char)
	 */
	@Override
	public int registerUser(String username, String firstName, String lastName,
			String password, String email, String gender) {
		return rp.parseRegisterUser(username, firstName, lastName, password, email, gender);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#removeUser(java.lang.String)
	 */
	@Override
	public int removeUser(String username) {
		return rp.parseRemoveUser(username);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#login(java.lang.String, java.lang.String)
	 */
	@Override
	public int login(String username, String password) {
		return rp.parseLogin(username, password);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#isModerator(java.lang.String)
	 */
	@Override
	public boolean isModerator(String username) {
		if(rp.parseIsModerator(username) == 1) {
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#getForums()
	 */
	@Override
	public List<Forum> getForums() {
		return rp.parseGetForums();
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#addForum(java.lang.String)
	 */
	@Override
	public int addForum(String subject, String username) {
		return this.rp.parseAddForum(subject);
	}
	
	public int addForumGui(String subject) {
		return this.rp.parseAddForum(subject);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#removeForum(int)
	 */
	@Override
	public int removeForum(int forumID) {
		return this.rp.parseRemoveForum(forumID);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#getThreads(int)
	 */
	@Override
	public List<forum.Thread> getThreads(int ForumID) {
		return this.rp.parseGetThreads(ForumID);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#addThread(java.lang.String, int, java.lang.String, java.lang.String)
	 */
	@Override
	public int addThread(String username, int ForumID,
			String subject, String content, String date) {
		return this.rp.parseAddThread(username, ForumID, subject, content, date);
	}
	
	public int addThreadGui(String username, int ForumID,
			String subject, String content, String date) {
		return this.rp.parseAddThreadGui(username, ForumID, subject, content, date);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#removeThread(int, int, java.lang.String)
	 */
	@Override
	public int removeThread(int threadID) {
		return this.rp.parseRemoveThread(threadID);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#addPost(int, int, java.lang.String, java.lang.String, java.lang.String, int)
	 */
	@Override
	public int addReply(int PostIDreplyTo, String username,
			String subject, String content, String date) {
		return this.rp.parseAddReply(PostIDreplyTo, username, subject, content, date);
	}
	
	/**
	 * @param PostIDreplyTo
	 * @param username
	 * @param subject
	 * @param content
	 * @param date
	 * @return the number of the post that was added and 0 if fails
	 */
	public int addReplyGui(int PostIDreplyTo, String username,
			String subject, String content, String date) {
		return this.rp.parseAddReplyGui(PostIDreplyTo, username, subject, content, date);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#removePost(int, int, int)
	 */
	@Override
	public int removeReply(int PostID) {
		return this.rp.parseRemoveReply(PostID);
	}

	/* (non-Javadoc)
	 * @see database.databaseInterface#getThread(int, int)
	 */
	@Override
	public List<Post> getThread(int threadID) {
		return this.rp.parseGetThread(threadID);
	}

	/* (non-Javadoc)
	 * @see database.databaseInterface#getPost(int, int, int)
	 */
	@Override
	public Post getPost(int PostID) {
		return this.rp.parseGetPost(PostID);
	}

	/* (non-Javadoc)
	 * @see database.databaseInterface#editPost(int, int, int, java.lang.String)
	 */
	@Override
	public int editPost(int postID, String content) {
		return this.rp.parseEditPost(postID, content);
	}

	/* (non-Javadoc)
	 * @see database.DatabaseInterface#getThreads()
	 */
	@Override
	public List<Integer> getThreadsIds() {
		return this.rp.parseGetThreadsIds();
	}

	@Override
	public Member getInfoAboutFriend(String friendName) {
		return this.rp.parseGetInfoAboutFriend(friendName);
	}
}